package cn.iocoder.yudao.module.recycl.controller.admin.Itemtype;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.recycl.controller.admin.Itemtype.vo.*;
import cn.iocoder.yudao.module.recycl.dal.dataobject.Itemtype.ItemTypeDO;
import cn.iocoder.yudao.module.recycl.service.Itemtype.ItemTypeService;

@Tag(name = "管理后台 - 回收物品分类树")
@RestController
@RequestMapping("/recycl/item-type")
@Validated
public class ItemTypeController {

    @Resource
    private ItemTypeService itemTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建回收物品分类树")
    @PreAuthorize("@ss.hasPermission('recycl:item-type:create')")
    public CommonResult<Long> createItemType(@Valid @RequestBody ItemTypeSaveReqVO createReqVO) {
        return success(itemTypeService.createItemType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新回收物品分类树")
    @PreAuthorize("@ss.hasPermission('recycl:item-type:update')")
    public CommonResult<Boolean> updateItemType(@Valid @RequestBody ItemTypeSaveReqVO updateReqVO) {
        itemTypeService.updateItemType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除回收物品分类树")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('recycl:item-type:delete')")
    public CommonResult<Boolean> deleteItemType(@RequestParam("id") Long id) {
        itemTypeService.deleteItemType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得回收物品分类树")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('recycl:item-type:query')")
    public CommonResult<ItemTypeRespVO> getItemType(@RequestParam("id") Long id) {
        ItemTypeDO itemType = itemTypeService.getItemType(id);
        return success(BeanUtils.toBean(itemType, ItemTypeRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得回收物品分类树列表")
    @PreAuthorize("@ss.hasPermission('recycl:item-type:query')")
    public CommonResult<List<ItemTypeRespVO>> getItemTypeList(@Valid ItemTypeListReqVO listReqVO) {
        List<ItemTypeDO> list = itemTypeService.getItemTypeList(listReqVO);
        return success(BeanUtils.toBean(list, ItemTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出回收物品分类树 Excel")
    @PreAuthorize("@ss.hasPermission('recycl:item-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportItemTypeExcel(@Valid ItemTypeListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<ItemTypeDO> list = itemTypeService.getItemTypeList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "回收物品分类树.xls", "数据", ItemTypeRespVO.class,
                        BeanUtils.toBean(list, ItemTypeRespVO.class));
    }

}